<?php 
	
namespace app\invest\controller;
use think\Db; // 引用DB类
use think\Request;
use think\Session;

class Invest
{

    // http://127.0.0.1:85/index.php?s=/tdled/test
    /*
    {
        "GDH": "AK1970254",
        "LSM": "002"
    }
	*/
    public function index(){
    	
    }
    
    //先上传图片，随后将图片地址回传
    public function investImg(){
    	$invest_img		= request()->file('file');
    	$info 			= $invest_img->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'invest_img');
    	
    	// 图片保存后会根据日期再生成一个以上传日期命名的文件夹，要将路径存入数据库
    	$date 			= '20'.date('ymd'); 
    	$invest_img_url 	=  "http://127.0.0.1:88/static/invest_img/".$date."/".$info->getFilename();
    	
    	//将图片地址存入数据库
    	$isSave			= Db::table("invest_img")
    				   -> insert([
    				   		"invest_img_url"		=> $invest_img_url,
    				   		"invest_img_addTime"	=> Db::raw("now()")
    				   ]);
    	if($isSave){
    		return json(["url"=>$invest_img_url,"code"=>200]);
    	}else{
    		return json(["url"=>$invest_img_url,"code"=>500]);
    	}
    	
    }
    
    //获取店铺旺旺号=》下拉框
    public function select($user){
    	$role		= Db::table("login")
    			   -> where("login_name",$user)
    			   -> select();
    	if($role[0]["register_role"]==2){
    		$select	= Db::table("count")
	    		   -> where("user_name",$user)
	    		   -> select();
    	}else{
    		$select	= Db::table("count")
    		   -> select();
    	}
    	
    	return json(["data"=>$select]);
    }
    
    //发布任务活动
    public function addInvest(){
    	$invest_type		= request()->post("invest_type");//存储充值方式的id
    	$invest_money		= request()->post("invest_money");
    	$invest_img_url		= request()->post("invest_img_url");
    	$user_name			= request()->post("user_name");
    	
    	$invest_id			= '20'.date('ymdhis'); 
    	//var_dump($invest_id);
    	
    	$investAdd				= Db::table("invest")
    						   -> insert(
    						   		[
    						   			"invest_type"		=> $invest_type,
    						   			"invest_money"		=> $invest_money,
    						   			"invest_img_url"	=> $invest_img_url,
    						   			"invest_addTime"	=> Db::raw("now()"),
    						   			"user_name"			=> $user_name,
    						   			"invest_id"			=> $invest_id,
    						   			"invest_status"		=> 0
    						   		]);
    }
    
    //编辑任务
    public function editRenwu(){
    	$renwu_shop_type		= request()->post("renwu_shop_type");
    	$renwu_xiadan			= request()->post("renwu_xiadan");
    	$renwu_good_shop		= request()->post("renwu_good_shop");
    	$renwu_url				= request()->post("renwu_url");
    	$renwu_good_titles		= request()->post("renwu_good_titles");
    	$renwu_img_url			= request()->post("renwu_img_url");
    	$renwu_good_price		= request()->post("renwu_good_price");
    	$renwu_good_payPrice	= request()->post("renwu_good_payPrice");
    	$renwu_good_searchWord	= request()->post("renwu_good_searchWord");
    	$renwu_good_money		= request()->post("renwu_good_money");
    	$renwu_yaoqiu			= request()->post("renwu_yaoqiu");
    	$renwu_id				= request()->post("renwu_id");
    	
    	$renwuAdd				= Db::table("renwu")
    						   -> where("renwu_id",$renwu_id)
    						   -> update(
    						   		[
    						   			"renwu_shop_type"		=> $renwu_shop_type,
    						   			"renwu_xiadan"			=> $renwu_xiadan,
    						   			"renwu_good_shop"		=> $renwu_good_shop,
    						   			"renwu_url"				=> $renwu_url,
    						   			"renwu_good_titles"		=> $renwu_good_titles,
    						   			"renwu_img_url"			=> $renwu_img_url,
    						   			"renwu_good_price"		=> $renwu_good_price,
    						   			"renwu_good_payPrice"	=> $renwu_good_payPrice,
    						   			"renwu_good_searchWord"	=> $renwu_good_searchWord,
    						   			"renwu_good_money"		=> $renwu_good_money,
    						   			"renwu_yaoqiu"			=> $renwu_yaoqiu,
    						   			"renwu_addTime"			=> Db::raw("now()")
    						   		]);
    }
    
    //删除单个任务
    public function delRenwu($id){
    	$isDel	= Db::table("renwu")
    		   -> where("renwu_id",$id)
    		   -> delete();
    		   
    	if($isDel){
    		return json(["msg"=>"删除成功","code"=>200]);
    	}else{
    		return json(["msg"=>"请重试","code"=>500]);
    	}
    }
    
    //批量删除
    public function delMoreRenwu($renwu_id){
    	foreach($renwu_id as $renwu_id){
			$del	= Db::table('renwu')
			   	   -> where('renwu_id', $renwu_id)
			   	   -> delete();
		}
    }
    
    //搜索任务
    public function renwuSearch(){
    	$key			= request()->post("key");
    	$user_name	= request()->post("user_name");
		$isRenwu		= Db::table("renwu")
						-> where("user_name",$user_name)
						-> where("renwu_good_shop|renwu_good_searchWord|renwu_good_money","like","%".$key."%")
				   		-> order("renwu_addTime desc")
				   		-> select();
		$count			= count($isRenwu);		   
		return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$isRenwu]);
    }
    
    //返回任务列表
    public function investList($id){
    	$role		= Db::table("login")
    			   -> where("login_name",$id)
    			   -> select();
    	if($role[0]["register_role"]==2){
    		$investList	= Db::table("invest")
	    			   -> alias("i")
	    			   -> join("count c","c.count_id=i.invest_type")
	    			   -> where("i.user_name",$id)
	    			   -> order("i.invest_addtime desc")
	    			   -> select();
	    	$count		= count($investList);
    	}else{
    		$investList	= Db::table("invest")
	    			   -> alias("i")
	    			   -> join("count c","c.count_id=i.invest_type")
	    			   -> order("i.invest_addtime desc")
	    			   -> select();
	    	$count		= count($investList);
    	}
    	
    	//var_dump($investList);
    	return json(["code"=>0,"msg"=>"","count"=>$count,"data"=>$investList]);
    } 
    
    //充值审核
    public function checkInvest(){
    	$status			= request()->post("invest_check");
    	$desc			= request()->post("check_desc");
    	$invest_id		= request()->post("invest_id");
    	$user_name		= request()->post("user_name");
    	$invest_name	= request()->post("invest_name");
    	$invest_money	= request()->post("invest_money");
    	if($status=="no"){
    		$status		= Db::table("invest")
    				   -> where("invest_id",$invest_id)
    				   -> update(
    				   		[
    				   			"invest_status"	=> "充值失败",
    				   			"check_desc"	=> $desc
    				   		]);
    	}else{
    		$status		= Db::table("invest")
    				   -> where("invest_id",$invest_id)
    				   -> update(
    				   		[
    				   			"invest_status"	=> "充值成功",
    				   			"check_desc"	=> $desc
    				   		]);
    		
    		//充值成功，用户余额增加
    		if($status){
    			$money	= Db::table("balance")
    				   -> where("user_name",$invest_name)
    				   -> select();
    			$jine	= $money[0]["balance_money"];
    			$sum	= (float)$jine+(float)$invest_money;
    			
    			$all	= Db::table("balance")
    				   -> where("user_name",$invest_name)
    				   -> update(["balance_money"=>$sum]);
    		}
    	}
    }
}
